
#include "cuda_util.h"

#include <cuda.h>
#include <stdio.h>
#include <unistd.h>


/////////////////////////////////////////////////////
///////////////////DEBUGGING/////////////////////////
/////////////////////////////////////////////////////

// performance checking


void checkCUDAError(const char *msg, int i)
{
    cudaError_t err = cudaGetLastError();
    if( cudaSuccess != err) 
    {
        fprintf(stderr, "Cuda error: %s: %s.\n", msg, 
                                  cudaGetErrorString( err) );
        int len=256;
        char hostname[256];
        gethostname(hostname,len); 
        fprintf(stderr, "Error occured on %s, device %d.\n", hostname, i);        
        exit(EXIT_FAILURE);
    }                         
}

void checkCUDAError(const char *msg, int i, int j)
{
    cudaError_t err = cudaGetLastError();
    if( cudaSuccess != err) 
    {
        fprintf(stderr, "Cuda error: %s: %s.\n", msg, 
                                  cudaGetErrorString( err) );
        int len=256;
        char hostname[256];
        gethostname(hostname,len); 
        fprintf(stderr, "Error occured on %s, devices %d and %d.\n", hostname, i, j);
        exit(EXIT_FAILURE);
    }                         
}

// memCheck
void memCheck(void){
  size_t free,total;
  cuMemGetInfo(&free, &total);
  fprintf(stderr, "Free memory %f MB \n", free/ 1024.0 / 1024.0);
  fprintf(stderr, "Total memory %f MB \n", total/ 1024.0 / 1024.0);
  if(total > 0){
    fprintf(stderr, "Percent used %f %% \n", 100*float(total-free)/float(total));
  }else {
    fprintf(stderr, "Percent used 0 %% \n");
  }
}

/* vim: set sw=4 sts=4 et foldmethod=syntax syntax=c : */
